package com.shigen.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shigen.demo.entity.Visitors;
import com.shigen.demo.entity.req.VisitorsReq.QueryItem;
import com.shigen.demo.mapper.VisitorsMapper;
import com.shigen.demo.service.IVisitorsService;
import com.shigen.demo.util.IPUtil;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

@Service
public class VisitorsServiceImpl extends ServiceImpl<VisitorsMapper, Visitors> implements
    IVisitorsService {

  @Override
  public IPage<Visitors> getByPage(QueryItem queryItem) {
    Page<Visitors> page = new Page<>(queryItem.getPagenum(), queryItem.getPagesize());
    return lambdaQuery().orderByDesc(Visitors::getCreateTime).eq(Visitors::getIsDel, false).page(page);
  }

  @Override
  public int addVisitor(Visitors visitors, HttpServletRequest request) {
    String ip = IPUtil.getIp(request);
    String cityInfo = null;
    try {
      cityInfo = IPUtil.getCityInfo(ip);
    } catch (IOException e) {
      e.printStackTrace();
    }
    visitors.setIp(ip).setPosition(cityInfo);

    return getBaseMapper().insert(visitors);
  }
}
